1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8">
5 <meta http-equiv=
"Content-Style-Type" content=
"text/css">
7 <meta name=
"Generator" content=
"Cocoa HTML Writer">
8 <meta name=
"CocoaVersion" content=
"1038.25">
9 <style type=
"text/css">
10 p
.p1
{margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica
}
11 p
.p2
{margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica
; min-height: 17.0px}
12 p
.p3
{margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica
}
13 p
.p4
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
; min-height: 14.0px}
14 p
.p5
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
}
15 p
.p6
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
}
16 p
.p7
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; min-height: 12.0px}
17 p
.p8
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #606060}
18 p
.p9
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #0000bf}
19 p
.p10
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #bf0000}
20 p
.p11
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #007300}
21 p
.p12
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
; color: #0000ee}
22 span
.s1
{color: #0000bf}
23 span
.s2
{color: #007300}
24 span
.s3
{color: #000000}
25 span
.s4
{font: 12.0px Helvetica
}
26 span
.Apple-tab-span
{white-space:pre
}
30 <p class=
"p1"><b>Cookbook: Sequencing basics
</b></p>
31 <p class=
"p2"><br></p>
32 <p class=
"p3"><b>Playing a predefined note sequence
</b></p>
33 <p class=
"p4"><br></p>
34 <p class=
"p5">The following are three different ways of playing the same famous fugue subject.
</p>
35 <p class=
"p4"><br></p>
36 <p class=
"p5">The first is brute force, listing all the scale degrees mechanically in order. The second and third recognize the A pedal point and use interlacing operations to insert the pedal notes in between the changing notes.
</p>
37 <p class=
"p4"><br></p>
38 <p class=
"p5">The example demonstrates the use of the 'scale' and 'root' event keys to define tonality. Root =
2 is D, and the scale defines a natural minor mode. The degree sequence also uses accidentals. Subtracting
0.1 from an integer scale degree flattens the note by a semitone; adding
0.1 raises by a semitone. -
0.9 is
0.1 higher than -
1; a natural minor scale degree below the tonic is a flat
7, and a half step higher than that is the leading tone.
</p>
39 <p class=
"p4"><br></p>
41 <p class=
"p6"><span class=
"s1">TempoClock
</span>.default.tempo =
84/
60;
</p>
42 <p class=
"p7"><br></p>
43 <p class=
"p6">p =
<span class=
"s1">Pbind
</span>(
</p>
44 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\scale
</span>, #[
0,
2,
3,
5,
7,
8,
10],
</p>
45 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\root
</span>,
2,
</p>
46 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\degree
</span>,
<span class=
"s1">Pseq
</span>(#[rest,
4,
3,
4,
2,
4,
1,
4,
0,
4, -
0.9,
4,
0,
4,
1,
4,
2,
4,
</p>
47 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>-
3,
4, -
1.9,
4, -
0.9,
4,
0,
4, -
0.9,
4,
0,
4,
1,
4,
2],
1),
</p>
48 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\dur
</span>,
0.25</p>
49 <p class=
"p6">).play;
</p>
51 <p class=
"p7"><br></p>
53 <p class=
"p6">p =
<span class=
"s1">Pbind
</span>(
</p>
54 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\scale
</span>, #[
0,
2,
3,
5,
7,
8,
10],
</p>
55 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\root
</span>,
2,
</p>
56 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\degree
</span>,
<span class=
"s1">Place
</span>([#[rest,
3,
2,
1,
0, -
0.9,
0,
1,
2, -
3, -
1.9, -
0.9,
0, -
0.9,
0,
1,
2],
</p>
57 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>(
4 !
16) ++
<span class=
"s2">\rest
</span>],
17),
</p>
58 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\dur
</span>,
0.25</p>
59 <p class=
"p6">).play;
</p>
61 <p class=
"p7"><br></p>
63 <p class=
"p6">p =
<span class=
"s1">Pbind
</span>(
</p>
64 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\scale
</span>, #[
0,
2,
3,
5,
7,
8,
10],
</p>
65 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\root
</span>,
2,
</p>
66 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\degree
</span>,
<span class=
"s1">Ppatlace
</span>([
<span class=
"s1">Pseq
</span>(#[rest,
3,
2,
1,
0, -
0.9,
0,
1,
2, -
3, -
1.9, -
0.9,
0, -
0.9,
0,
1,
2],
1),
</p>
67 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"s1">Pn
</span>(
4,
16)],
<span class=
"s1">inf
</span>),
</p>
68 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\dur
</span>,
0.25</p>
69 <p class=
"p6">).play;
</p>
71 <p class=
"p4"><br></p>
72 <p class=
"p4"><br></p>
73 <p class=
"p3"><b>“Multichannel” expansion
</b></p>
74 <p class=
"p4"><br></p>
75 <p class=
"p5">In a SynthDef, using an array as the input to a UGen expands the UGen into an array of UGens (see
<a href=
"../../Other Topics/MultiChannel.html"><span class=
"s1">MultiChannel
</span></a>). Something similar happens in patterns. Normally a value sent to a Synth node should be a single number, but if it's an array instead, the pattern expands the event to produce
<i>multiple synth nodes
</i> instead of just one.
</p>
76 <p class=
"p4"><br></p>
77 <p class=
"p5">The 'degree' pattern applies a set of chord intervals to a melody that's always on top. It's a compound pattern, Pseries(...) + Prand(...), where Pseries returns a single number and Prand returns an array. As with regular math operations, a number plus an array is an array. If the current Pseries value is
7 and Prand returns [
0, -
3, -
5], the result is [
7,
4,
2] and you would hear a C major triad in first inversion.
</p>
78 <p class=
"p4"><br></p>
80 <p class=
"p6">p =
<span class=
"s1">Pbind
</span>(
</p>
81 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\degree
</span>,
<span class=
"s1">Pseries
</span>(
7,
<span class=
"s1">Pwhite
</span>(
1,
3,
<span class=
"s1">inf
</span>) *
<span class=
"s1">Prand
</span>(#[-
1,
1],
<span class=
"s1">inf
</span>),
<span class=
"s1">inf
</span>).fold(
0,
14)
</p>
82 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>+
<span class=
"s1">Prand
</span>(#[[
0, -
2, -
4], [
0, -
3, -
5], [
0, -
2, -
5], [
0, -
1, -
4]],
<span class=
"s1">inf
</span>),
</p>
83 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\dur
</span>,
<span class=
"s1">Pwrand
</span>(#[
1,
0.5], #[
0.8,
0.2],
<span class=
"s1">inf
</span>)
</p>
84 <p class=
"p6">).play;
</p>
86 <p class=
"p7"><br></p>
87 <p class=
"p6">p.stop;
</p>
88 <p class=
"p4"><br></p>
89 <p class=
"p4"><br></p>
90 <p class=
"p3"><b>Using custom SynthDefs (including unpitched SynthDefs)
</b></p>
91 <p class=
"p4"><br></p>
92 <p class=
"p5">Patterns have special features to support several styles of pitch organization, but those features are strictly optional. Here we play a SynthDef that has no frequency argument whatsoever.
</p>
93 <p class=
"p4"><br></p>
94 <p class=
"p5">Note the use of
<b>add
</b> to prepare the SynthDef. Without it, most of the SynthDef inputs would not be recognized and the pattern would not send values to them.
</p>
95 <p class=
"p4"><br></p>
96 <p class=
"p5">It's worth noting that the pattern runs in beats, whose real duration in seconds depends on the clock's tempo. The SynthDef, however, always measures time in seconds. This example keeps things simple by setting the clock to
1 beat per second. If the tempo needs to be something else, though, the 'time' key should be divided by the tempo:
</p>
97 <p class=
"p4"><br></p>
98 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\time
</span>,
<span class=
"s1">Pkey
</span>(
<span class=
"s2">\delta
</span>) /
<span class=
"s1">Pfunc
</span> {
<span class=
"s1">thisThread
</span>.clock.tempo },
</p>
99 <p class=
"p4"><br></p>
100 <p class=
"p4"><br></p>
102 <p class=
"p8"><span class=
"s3">b =
</span><span class=
"s1">Buffer
</span><span class=
"s3">.read(s,
</span>"sounds/a11wlk01.wav"<span class=
"s3">);
</span></p>
103 <p class=
"p7"><br></p>
104 <p class=
"p9">SynthDef
<span class=
"s3">(
</span><span class=
"s2">\stretchedFragments
</span><span class=
"s3">, {
</span>|out, bufnum, start, time =
1, stretch =
1, amp =
1, attack =
0.01, decay =
0.05|
</p>
105 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s1">var
</span><span class=
"Apple-tab-span"> </span>sig =
<span class=
"s1">PlayBuf
</span>.ar(
1, bufnum, rate: stretch.reciprocal, startPos: start, doneAction:
2);
</p>
106 <p class=
"p6"><span class=
"Apple-tab-span"> </span>sig =
<span class=
"s1">PitchShift
</span>.ar(sig, pitchRatio: stretch)
</p>
107 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>*
<span class=
"s1">EnvGen
</span>.kr(
<span class=
"s1">Env
</span>.linen(attack, time, decay), doneAction:
2);
</p>
108 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s1">Out
</span>.ar(out, sig !
2)
</p>
109 <p class=
"p10"><span class=
"s3">}).add;
<span class=
"Apple-tab-span"> </span></span>// note add! Without this, arguments won't work
</p>
111 <p class=
"p7"><br></p>
113 <p class=
"p6"><span class=
"s1">TempoClock
</span>.default.tempo =
1;
</p>
114 <p class=
"p7"><br></p>
115 <p class=
"p6">p =
<span class=
"s1">Pbind
</span>(
</p>
116 <p class=
"p11"><span class=
"s3"><span class=
"Apple-tab-span"> </span></span>\instrument
<span class=
"s3">,
</span>\stretchedFragments
<span class=
"s3">,
</span></p>
117 <p class=
"p11"><span class=
"s3"><span class=
"Apple-tab-span"> </span></span>\bufnum
<span class=
"s3">, b,
</span></p>
118 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\start
</span>,
<span class=
"s1">Pwhite
</span>(
0, (b.numFrames *
0.7).asInteger,
<span class=
"s1">inf
</span>),
</p>
119 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\delta
</span>,
<span class=
"s1">Pexprand
</span>(
0.2,
1.5,
<span class=
"s1">inf
</span>),
</p>
120 <p class=
"p11"><span class=
"s3"><span class=
"Apple-tab-span"> </span></span>\time
<span class=
"s3">,
</span><span class=
"s1">Pkey
</span><span class=
"s3">(
</span>\delta
<span class=
"s3">),
</span></p>
121 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\stretch
</span>,
<span class=
"s1">Pexprand
</span>(
1.0,
4.0,
<span class=
"s1">inf
</span>),
</p>
122 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\amp
</span>,
0.5,
</p>
123 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\attack
</span>,
0.1,
</p>
124 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">\decay
</span>,
0.2</p>
125 <p class=
"p6">).play;
</p>
127 <p class=
"p7"><br></p>
128 <p class=
"p6">p.stop;
</p>
129 <p class=
"p10"><span class=
"s3">b.free;
<span class=
"Apple-tab-span"> </span></span>// be tidy! remember to clean up your Buffer
</p>
130 <p class=
"p4"><br></p>
131 <p class=
"p4"><br></p>
132 <p class=
"p12"><span class=
"s3">Previous:
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><a href=
"PG_08_Event_Types_and_Parameters.html"><span class=
"s4">PG_08_Event_Types_and_Parameters
</span></a></span></p>
133 <p class=
"p12"><span class=
"s3">Next:
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><a href=
"PG_Cookbook02_Manipulating_Patterns.html"><span class=
"s4">PG_Cookbook02_Manipulating_Patterns
</span></a></span></p>